package com.dudu.utils;



import java.io.File;
import java.io.InputStream;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;

@SuppressWarnings("deprecation")
public class HttpClientCenter {

	/**
	 * http clent 公共类
	 */
	public String httpUrl = "";

	public HttpClientCenter() {
	}

	public HttpClientCenter(String httpUrl) {
		this.httpUrl = httpUrl;
	}

	public String getHttpUrl() {
		return httpUrl;
	}

	public void setHttpUrl(String httpUrl) {
		this.httpUrl = httpUrl;
	}

	/**
	 * post方式 json对像
	 */
	public JSONObject post(JSONObject json) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(httpUrl);
		try {
			post.addRequestHeader("Content-Type",
					"application/json;charset=utf-8");// 在头文件中设置转码
			post.setRequestBody(json.toString());
			int statusCode = client.executeMethod(post);
			if (statusCode == HttpStatus.SC_OK) {// 成功返回
				InputStream inStream = post.getResponseBodyAsStream();
				byte[] data = HttpRequest.readStream(inStream);
				String responseString = new String(data, "utf-8");
				return JSONObject.fromObject(responseString);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			post.releaseConnection();
		}
	}
	
	
	public String postHttps(String jsonStr,String contentType) {
		
		HttpClient client = new HttpClient();
		Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443); 
		Protocol.registerProtocol("https", myhttps); 
		PostMethod post = new PostMethod(httpUrl);
		try {
			post.addRequestHeader("Content-Type",
					contentType+";charset=utf-8");// 在头文件中设置转码
			post.setRequestBody(jsonStr);
			int statusCode = client.executeMethod(post);
			if (statusCode == HttpStatus.SC_OK) {// 成功返回
				InputStream inStream = post.getResponseBodyAsStream();
				byte[] data = HttpRequest.readStream(inStream);
				String responseString = new String(data, "utf-8");
				return responseString;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			post.releaseConnection();
		}
	}
	
	/**
	 * post方式 json对像
	 * Content-Type text/plain
	 */
	public JSONObject post(JSONObject json,String contentType) {
		HttpClient client = new HttpClient();
		Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443); 
		Protocol.registerProtocol("https", myhttps); 
		PostMethod post = new PostMethod(httpUrl);
		try {
			post.addRequestHeader("Content-Type",
					contentType+";charset=utf-8");// 在头文件中设置转码
			post.setRequestBody(json.toString());
			int statusCode = client.executeMethod(post);
			if (statusCode == HttpStatus.SC_OK) {// 成功返回
				InputStream inStream = post.getResponseBodyAsStream();
				byte[] data = HttpRequest.readStream(inStream);
				String responseString = new String(data, "utf-8");
				return JSONObject.fromObject(responseString);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			post.releaseConnection();
		}
	}

	// post方式上传附件
	@SuppressWarnings("deprecation")
	public String postFile(String... filePaths) {
		HttpClient client = new HttpClient();
		client.setTimeout(80000);
		MultipartPostMethod mPost = new MultipartPostMethod(httpUrl);
		try {
			for (String filePath : filePaths) {
				File file = new File(filePath);
				mPost.addParameter(file.getName(), file.getName(), file);
			}
			int statusCode = client.executeMethod(mPost);
			if (statusCode == HttpStatus.SC_OK) {// 成功返回
				InputStream inStream = mPost.getResponseBodyAsStream();
				byte[] data = HttpRequest.readStream(inStream);
				String responseString = new String(data, "utf-8");
				return responseString;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			mPost.releaseConnection();
		}
	}
	
	/**
	 * 
	 * get 方式
	 */
	
	

	public static void main(String[] args) {
		HttpClientCenter httpClientCenter = new HttpClientCenter(
				"http://172.20.6.49:8080/yongyouOneUniq0609/rs/userBusinessController/RegisterBusinessJson");
		JSONObject jSONObject = new JSONObject();
		jSONObject.put("userid", "chenweitest");
		jSONObject.put("username", "陈伟测试");
		jSONObject.put("corporationid", "01");
		jSONObject.put("corporationname", "测试");
		jSONObject.put("realname", "陈伟");
		jSONObject.put("identitycard", "000000000000000000");
		jSONObject.put("mobilephone", "17001090115");
		jSONObject.put("email", "chenweiz@yonyou.com");
		jSONObject.put("registerdate", "20150713095656");
		jSONObject.put("accountflag", "1");
		jSONObject.put("accountfrom", "");
		jSONObject.put("validateemail", "0");
		jSONObject.put("validatephone", "0");
		jSONObject.put("validateidcard", "0");
		jSONObject.put("payuser", "0");
		jSONObject.put("sortkey", "");
		jSONObject.put("userpsw", "yonyou");
		jSONObject.put("clientid", "");
		jSONObject.put("clientsecret", "");
		JSONObject returnJson = httpClientCenter.post(jSONObject);
	}

}
